package net.towee.client.com.stuff.view;

import java.util.Date;

import net.towee.client.com.stuff.presenter.GroupInformationShowerPresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.DataHelpers;
import net.towee.client.widgets.ExtFlowPanel;
import net.towee.client.widgets.SpanPanel;
import net.towee.model.group.GroupOpenness;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.inject.Inject;

public class GroupInformationView extends FlowPanel implements Display {
	private final ClientResource resource;
	private final ToweeConstants constants;
	
	private final Button enterToGroupButton = new Button();
	private final Button leaveFromGroupButton = new Button();
	private final Button changeButton = new Button();
	private final Button changeButtonH = new Button();
	
	private final FlexTable table = new FlexTable();
	private final TextBox question = new TextBox();
	private final SpanPanel usersNumber = new SpanPanel();
	private final SpanPanel creationDate = new SpanPanel();
	private final SpanPanel wrongAnswer = new SpanPanel();
	private final TextArea desc = new TextArea();
	private final TextBox name = new TextBox();
	private final TextBox alias = new TextBox();
	private final TextBox homeurl = new TextBox();
	private final TextBox answer = new TextBox();
	private final ListBox openness = new ListBox();
	private final Label questionBox = new Label();
	private boolean canChangeProfile = false;
	private boolean canChangeQuestion = false;

	@Inject
	public GroupInformationView(ToweeConstants constants, ClientResource resource) {
		this.constants = constants;
		this.resource = resource;
		
		// Set style
		setStyleName(resource.css().stuffGroupInfo() );
		
		// Add items
		openness.addItem(constants.group_fully_opened(), GroupOpenness.OPEN_GROUP+"");
		openness.addItem(constants.group_with_request(), GroupOpenness.WITH_REQUEST+"");
		openness.addItem(constants.group_closed(), GroupOpenness.HIDDEN_GROUP+"");
		
		// Create form with data
		createDataForm();
		disableEditing();
		
		// Set styles for change button
		changeButton.setStyleName( resource.css().lobbyNextStepButton() );
		changeButton.getElement().setAttribute("changeButton", "");
		changeButton.setText( constants.change() );
		changeButton.setVisible(false);
		add( changeButton );
		
		changeButtonH.setStyleName( resource.css().lobbyNextStepButton() );
		changeButtonH.getElement().setAttribute("changeButton", "");
		changeButtonH.setText( constants.done() );
		changeButtonH.setVisible(false);
		add( changeButtonH );
		
		changeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				changeButton.setVisible(false);
				changeButtonH.setVisible(true);
				enableEditing();
			}
		});
		
		changeButtonH.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				changeButton.setVisible(true);
				changeButtonH.setVisible(false);
				disableEditing();
			}
		});
	}
	
	private void disableEditing() {
		getElement().setAttribute("disabled", "disabled");
		
		table.setWidget(0, 0, new Label(desc.getText()));
		table.setWidget(1, 1, new Label(name.getText()));
		table.setWidget(2, 1, new Label(alias.getText()));
		table.setWidget(3, 1, new Label(homeurl.getText()));
		table.setWidget(4, 1, new Label( openness.getItemText(openness.getSelectedIndex())) );
		questionBox.setText( question.getText() );
		
		if(canChangeQuestion) {
			table.setWidget(5, 1, questionBox);
			table.setWidget(6, 1, new Label(answer.getText()));
		}
	}
	
	public void enableEditing() {
		getElement().removeAttribute("disabled");
		
		if(canChangeProfile) {
			table.setWidget(0, 0, desc);
			table.setWidget(1, 1, name);
			table.setWidget(2, 1, alias);
			table.setWidget(3, 1, homeurl);
			table.setWidget(4, 1, openness);
		}
		if(canChangeQuestion) {
			table.setWidget(5, 1, question);
			table.setWidget(6, 1, answer);
		}
	}

	private void createDataForm() {
		// Create table
		FlexCellFormatter cellFormatter = table.getFlexCellFormatter();
		table.addStyleName( resource.css().dataForm() );
		table.setCellSpacing(6);			
		   
		// Set colspan for desc
		cellFormatter.setColSpan(0, 0, 2);
		
		// Create table
		table.setWidget(0, 0, desc);
		table.setWidget(1, 0, getRowName( constants.group_name(), "Пример: Мат-Мех УрГУ, КН-202"));
		table.setWidget(1, 1, name);
		table.setWidget(2, 0, getRowName( constants.group_id(), "Пример: usucs2010_2"));
		table.setWidget(2, 1, alias);
		table.setWidget(3, 0, getRowName( constants.home_site(), "Пример: example.com"));
		table.setWidget(3, 1, homeurl);
		table.setWidget(4, 0, getRowName( constants.group_openness(), "Открытая - заходят все, Полуоткрытая - только с вашего согласия, Закрытая - никто не зайдет"));
		table.setWidget(4, 1, openness);
		
		cellFormatter.setWidth(0, 0, "100%");
		add(table);
	}
	
	private FlowPanel getRowName(String name, String desc){
		FlowPanel row = new FlowPanel();
		
		ExtFlowPanel nameSpan = new ExtFlowPanel(name);
		ExtFlowPanel descSpan = new ExtFlowPanel(desc);

		nameSpan.getElement().setAttribute("row", "name");
		descSpan.getElement().setAttribute("row", "desc");
		
		row.add(nameSpan);
		row.add(descSpan);
		
		return row;
	}

	@Override
	public void setDescription(String description) {
		desc.setText( description );
	}

	@Override
	public void setHomeUrl(String homeUrl) {
		homeurl.setText(homeUrl);
	}

	@Override
	public void setNumberOfUsers(int numberOfUsers) {
		usersNumber.setText( numberOfUsers+"" );
	}

	@Override
	public void setPrintName(String printName) {
		name.setText(printName);
	}

	@Override
	public void setAlias(String aliasText) {
		alias.setText(aliasText);
	}

	@Override
	public void setOpenness(GroupOpenness o) {
		switch(o) {
		case OPEN_GROUP: openness.setSelectedIndex(0); break;
		case WITH_REQUEST: openness.setSelectedIndex(1); break;
		case HIDDEN_GROUP: openness.setSelectedIndex(2); break;
		}
	}

	@Override
	public void setCreationDate(Date date) {
		creationDate.add( DataHelpers.formattedDateTimeWidget(date) );
	}

	@Override
	public void showLeaveGroupButton() {
		leaveFromGroupButton.setStyleName( resource.css().lobbyCloseButton() );
		leaveFromGroupButton.setText( constants.leave_from_group() );
		add(leaveFromGroupButton);
	}

	@Override
	public void showEnterToGroupButton() {
		FlowPanel enterArea = new FlowPanel();
		enterArea.getElement().setAttribute("enterArea", "");
		
		enterToGroupButton.setStyleName(resource.css().lobbyNextStepButton());
		enterToGroupButton.setText( constants.join_to_group() );
		wrongAnswer.setText( constants.wrong_answer() );
		wrongAnswer.setVisible(false);
		
		// Enable answer text area
		answer.setEnabled(true);
		questionBox.setText(questionBox.getText());
		
		enterArea.add(questionBox);
		FlowPanel answerBox = new FlowPanel();
		answerBox.setStyleName( resource.css().roundedInputBox() );
		answerBox.add(answer);
		enterArea.add(answerBox);
		
		FlowPanel wrong = new FlowPanel();
		wrong.add( wrongAnswer );
		wrong.getElement().setAttribute("wrong", "");

		enterArea.add(wrong);
		enterArea.add(enterToGroupButton);
		add(enterArea);		
	}

	@Override
	public void showSendRequestButton() {
		showEnterToGroupButton();
	}

	@Override
	public String getDescription() {
		return desc.getText();
	}

	@Override
	public String getHomeUrl() {
		return homeurl.getText();
	}

	@Override
	public String getPrintName() {
		return name.getText();
	}

	@Override
	public String getQuestAnswer() {
		return answer.getText();
	}

	@Override
	public GroupOpenness getOpenness() {
		return GroupOpenness.valueOf( openness.getValue( openness.getSelectedIndex() ) );
	}

	@Override
	public void showGroupQuestion(String result) {
		question.setText( result );
		questionBox.setText(result);
	}

	@Override
	public String getAlias() {
		return alias.getText();
	}

	@Override
	public void wrongAnswer() {
		wrongAnswer.setVisible(true);
	}

	@Override
	public void enteringDone() {
		enterToGroupButton.getParent().removeFromParent();
		answer.setEnabled(false);
		showLeaveGroupButton();
	}

	@Override
	public void requestingDone() {

	}

	@Override
	public void leaveGroupDone() {
		canNotChangeProfile();
		canNotChangeQuestion();
		
		leaveFromGroupButton.removeFromParent();
		disableEditing();
		changeButton.setVisible(false);
		changeButtonH.setVisible(false);
		showEnterToGroupButton();
	}

	@Override
	public HasClickHandlers getLeaveGropuButton() {
		return leaveFromGroupButton;
	}

	@Override
	public HasClickHandlers getEnterToGroupButton() {
		return enterToGroupButton;
	}

	@Override
	public HasClickHandlers getChangeButton() {
		return changeButtonH;
	}

	@Override
	public String getQuest() {
		return question.getText();
	}

	@Override
	public void setQuestAnswer(String result) {
		answer.setText( result );
		table.setWidget(6, 1, new Label(result));
	}

	@Override
	public void canChangeProfile() {
		this.canChangeProfile = true;
		changeButton.setVisible(true);
		
		disableEditing();
	}

	@Override
	public void canChangeQuestion() {
		table.setWidget(5, 0, getRowName( constants.control_quest(), "Тестовый вопрос для входа в группу. Пример: Как зовут нашего старасту?"));
		table.setWidget(5, 1, question);
		table.setWidget(6, 0, getRowName( constants.control_quest_answ(), "Пример: Вася"));
		table.setWidget(6, 1, answer);
		
		changeButton.setVisible(true);
		this.canChangeQuestion = true;
		
		disableEditing();
	}

	@Override
	public void canNotChangeProfile() {
		this.canChangeProfile = false;
		disableEditing();
		changeButton.setVisible(false);
		changeButtonH.setVisible(false);	
	}

	@Override
	public void canNotChangeQuestion() {
		canChangeQuestion=false;
		
		disableEditing();
		
		if(table.getRowCount() > 5) {
			table.removeRow(6);
			table.removeRow(5);
		}
	}
}
